package ide.ccf.ccproject.templateengine;

import ide.ccf.ccproject.common.WizardUtils;

import java.io.File;
import java.util.HashMap;
import java.util.Locale;
import java.io.IOException;
import java.io.InputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.net.URL;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.cdt.core.templateengine.TemplateCore;
import org.eclipse.cdt.core.templateengine.TemplateEngineHelper;
import org.eclipse.cdt.core.templateengine.process.ProcessRunner;
import org.eclipse.cdt.core.templateengine.process.ProcessArgument;
import org.eclipse.cdt.core.templateengine.process.ProcessFailureException;

import ide.ccf.ccproject.managedbuilder.*;
import ide.ccf.ccproject.wizards.pages.FilesWizardPage;

public class SwitchGuiProjectData extends ProcessRunner {
	
	public static  FilesWizardPage filesPage;

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public void process(TemplateCore template, ProcessArgument[] args, String processId,
			IProgressMonitor monitor) throws ProcessFailureException {
		URL path;
		try {
			path = TemplateEngineHelper.getTemplateResourceURLRelativeToTemplate(template, "/Gui/gui.pro");
			if (path == null) {
				throw new ProcessFailureException(getProcessMessage(processId, IStatus.ERROR, "gui.pro")); //$NON-NLS-1$
			}
		} catch (IOException e1) {
			throw new ProcessFailureException(getProcessMessage(processId, IStatus.ERROR, "gui.pro")); //$NON-NLS-1$
		}
		String[] nCount =  path.getPath().split("/");
		String Path = "";
		for(int i = 0; i < nCount.length -1; i++)
		{
			Path += nCount[i] +"/";
		}
		HashMap replaceMap = new HashMap();
		String hdr = filesPage.getHeaderFileName();
		if (hdr.endsWith(".h"))
			hdr = hdr.substring(0, hdr.length() - 2);
		
		String preDef = hdr.toUpperCase(Locale.US) + "_H";

		String uiHdr = filesPage.getUIFileName();
		
		if (uiHdr.endsWith(".ui"))
			uiHdr = uiHdr.substring(0, uiHdr.length() - 3);
		
		uiHdr = "ui_" + uiHdr + ".h";
		
		String headerFile = filesPage.getHeaderFileName();
		String srcFile = filesPage.getSourceFileName();
		String uiFile = filesPage.getUIFileName();
		String uiClass = filesPage.getUiClassName();
		String projectName = args[0].getSimpleValue();
		
		replaceMap.put("%MODULES%", "core gui xml");
		replaceMap.put("%INCLUDE%", headerFile);
		replaceMap.put("%CLASS%", filesPage.getClassName());
		replaceMap.put("%PRE_DEF%", preDef);
		replaceMap.put("%UI_HDR%", uiHdr);
		replaceMap.put("%UI_CLASS%", uiClass);
		replaceMap.put("%PROJECT%", projectName);
		replaceMap.put("%HEADER_FILE%", headerFile);
		replaceMap.put("%SOURCE_FILE%", srcFile);
		replaceMap.put("%UI_FILE%", uiFile);
		
		IProject projectHandle = ResourcesPlugin.getWorkspace().getRoot().getProject(projectName);
		File file = new File(path.getFile());
		InputStream src = null;
		try {
			src = new FileInputStream(file);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		File dest = new File(projectHandle.getLocation().toOSString() + "/" + projectName + ".pro");
		WizardUtils.addTemplateFile(src, dest, replaceMap);

		file = new File(Path + "gui.h");
		try {
			src = new FileInputStream(file);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		dest = new File(projectHandle.getLocation().toOSString() + "/" + headerFile);
		WizardUtils.addTemplateFile(src, dest, replaceMap);
		
		file = new File(Path + "gui.cpp");
		try {
			src = new FileInputStream(file);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		dest = new File(projectHandle.getLocation().toOSString() + "/" + srcFile);
		WizardUtils.addTemplateFile(src, dest, replaceMap);

		file = new File(Path + "main.cpp");
		try {
			src = new FileInputStream(file);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		dest = new File(projectHandle.getLocation().toOSString() + "/main.cpp");
		WizardUtils.addTemplateFile(src, dest, replaceMap);
		
		file = new File(Path + uiClass + ".ui");
		try {
			src = new FileInputStream(file);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		dest = new File(projectHandle.getLocation().toOSString() + "/" + uiFile);
		WizardUtils.addTemplateFile(src, dest, replaceMap);
		
	}
	
}
